﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX.Direct2D1;
using SharpDX;

namespace SFSpacefight
{
	public class Drawable2D
	{
		protected List<Bitmap> bitmaps; //multiple frames go downwards
		protected int width, height;

		protected DrawingRectangleF positionrect, lastpositionrect, drawpositionrect;
		protected DrawingPointF vector, lastvector;
		protected int index, lastindex, imagecount;

		public Drawable2D(List<Bitmap> inbitmaps, DrawingPointF positioncenter)
		{
			width = (int)inbitmaps[0].PixelSize.Width;
			height = (int)inbitmaps[0].PixelSize.Height;
			imagecount = inbitmaps.Count;

			bitmaps = inbitmaps;
			positionrect = new DrawingRectangleF(positioncenter.X - width * 0.5f, positioncenter.Y - height * 0.5f, positioncenter.X + width * 0.5f, positioncenter.Y + height * 0.5f);
			lastpositionrect = new DrawingRectangleF(positionrect.X, positionrect.Y, positionrect.X + positionrect.Width, positionrect.Y + positionrect.Height);
			drawpositionrect = new DrawingRectangleF(positionrect.X, positionrect.Y, positionrect.X + positionrect.Width, positionrect.Y + positionrect.Height);
			vector = new DrawingPointF();
			lastvector = new DrawingPointF();
			index = 0;
			lastindex = 0;
		}
		public GameState Loop()
		{
			lastindex = index;
			lastpositionrect.X = positionrect.X;
			lastpositionrect.Y = positionrect.Y;
			lastvector.X = vector.X;
			lastvector.Y = vector.Y;

			index += (index == bitmaps.Count - 1)? -index : 1;
			positionrect.X += vector.X;
			positionrect.Y += vector.Y;
			return GameState.None;
		}
		public void Draw(WindowRenderTarget target, float percent)
		{
			drawpositionrect.X = Methods.smoothstep(lastpositionrect.X - lastvector.X, lastpositionrect.X, positionrect.X, positionrect.X + vector.X, percent);
			drawpositionrect.Y = Methods.smoothstep(lastpositionrect.Y - lastvector.Y, lastpositionrect.Y, positionrect.Y, positionrect.Y + vector.Y, percent);
			
			target.DrawBitmap(bitmaps[lastindex], drawpositionrect, 1 - percent, BitmapInterpolationMode.Linear);// - percent);
			//target.DrawBitmap(bitmaps[index], drawpositionrect, percent, BitmapInterpolationMode.Linear);
		}
	}
}